package com.chen.blue.service.system.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.blue.converter.system.DeptConverter;
import com.chen.blue.converter.system.PostConverter;
import com.chen.blue.domain.delete.system.PostDelete;
import com.chen.blue.domain.dto.system.PostDto;
import com.chen.blue.domain.dto.system.update.PostDtoEdit;
import com.chen.blue.domain.model.system.DeptModel;
import com.chen.blue.domain.model.system.PostModel;
import com.chen.blue.domain.query.system.PostQuery;
import com.chen.blue.domain.vo.system.DeptVo;
import com.chen.blue.domain.vo.system.PostVo;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.mapper.system.PostMapper;
import com.chen.blue.result.DataResult;
import com.chen.blue.service.candidate.CandidateInfoService;
import com.chen.blue.service.system.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.blue.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author CLT
 * @since 2024-05-10
 */
@Service("iPostService")
public class IPostServiceImpl extends ServiceImpl<PostMapper, PostModel> implements IPostService {


    @Override
    public boolean addPost(PostDto postDto) throws DtoNullException {

        if (Objects.isNull(postDto)) {
            throw new DtoNullException("参数为空");
        }

        CandidateInfoService.setCreator(postDto);

        return super.save(PostConverter.dtoToModel(postDto));

    }

    @Override
    public boolean deletePost(PostDelete postDelete) throws DtoNullException {

        if (Objects.isNull(postDelete)) {
            throw new DtoNullException("参数为空");
        }

        Map<String, Object> wrap = new HashMap<>();
        if (Objects.nonNull(postDelete.getId())) {
            wrap.put("id", postDelete.getId());
        }

        return Objects.nonNull(postDelete.getIds()) ? super.removeByIds(postDelete.getIds()) : super.removeByMap(wrap);

    }

    @Override
    public boolean updatePostById(PostDtoEdit postDtoEdit) throws DtoNullException {

        if (Objects.isNull(postDtoEdit)) {
            throw new DtoNullException("参数为空");
        }

        CandidateInfoService.setUpdater(postDtoEdit);

        return super.updateById(PostConverter.dtoEditToModel(postDtoEdit));

    }

    @Override
    public DataResult<List<PostVo>> queryPost(PostQuery postQuery) throws DtoNullException {

        if (Objects.isNull(postQuery)) {
            throw new DtoNullException("查询条件为空");
        }

        LambdaQueryWrapper<PostModel> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(postQuery.getPageNo()) && Objects.nonNull(postQuery.getPageSize())) {
            PageHelper.startPage(postQuery.getPageNo(), postQuery.getPageSize());
        }
        if (Objects.nonNull(postQuery.getCode()) && StrUtil.isNotBlank(postQuery.getCode())) {
            wrapper.like(PostModel::getCode, postQuery.getCode());
        }
        if (Objects.nonNull(postQuery.getName()) && StrUtil.isNotBlank(postQuery.getName())) {
            wrapper.like(PostModel::getName, postQuery.getName());
        }
        if (Objects.nonNull(postQuery.getStatus())) {
            wrapper.eq(PostModel::getStatus, postQuery.getStatus());
        }
        wrapper.orderByDesc(PostModel::getCreateTime);

        List<PostModel> postModelList = super.list(wrapper);
        PageInfo<PostModel> pageInfo = new PageInfo<>(postModelList);
        PageHelper.clearPage();

        /**
         * 统一结果返回
         */
        DataResult<List<PostVo>> dataResult = new DataResult<>();
        dataResult.setData(PostConverter.modelToVoList(pageInfo.getList()));
        PageInfoUtil.setPageInfo(dataResult, pageInfo);

        return dataResult;

    }

    @Override
    public DataResult<PostVo> queryPostById(Long id) throws DtoNullException {

        if (Objects.isNull(id)) {
            throw new DtoNullException("ID为空");
        }

        PostModel postModel = super.getById(id);

        DataResult<PostVo> dataResult = new DataResult<>();
        dataResult.setData(PostConverter.modelToVo(postModel));

        return dataResult;

    }


}
